package com.mikufufu.blog.controller.admin;

import com.mikufufu.blog.model.entity.Permission;
import com.mikufufu.blog.model.vo.admin.PermissionVO;
import com.mikufufu.blog.model.vo.admin.PermissionRoleListVO;
import com.mikufufu.blog.service.PermissionService;
import org.springframework.web.bind.annotation.*;
import com.mikufufu.blog.common.entity.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/permission")
@Api(tags = "权限接口")
public class PermissionAdminController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取权限详情")
    @ApiImplicitParam(name = "id", value = "权限id", required = true)
    public  AjaxResult<Permission> detail(Integer id) {
        return AjaxResult.data(permissionService.getById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取指定角色的权限列表")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
    public AjaxResult<Set<String>> list(Integer roleId) {
        return AjaxResult.data(permissionService.getPermissionSet(roleId).stream().filter(permission -> permission.getSign() != null && permission.getStatus() == 0 ).map(Permission::getSign).collect(Collectors.toSet()));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增权限")
    public AjaxResult<Boolean> add(@RequestBody Permission permission) {
        return AjaxResult.status(permissionService.save(permission));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改权限")
    public AjaxResult<Boolean> update(@RequestBody Permission permission) {
        return AjaxResult.status(permissionService.updateById(permission));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除权限")
    @ApiImplicitParam(name = "id", value = "权限id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.status(permissionService.removeById(id));
    }

    @GetMapping("/addRolePermission")
    @ApiOperation(value = "为指定角色新增权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true),
            @ApiImplicitParam(name = "permissionId", value = "权限id", required = true)
    })
    public AjaxResult<Boolean> addRolePermission(Integer roleId, Integer permissionId) {
        return AjaxResult.status(permissionService.addRolePermission(roleId, permissionId));
    }

    @GetMapping("/getPermissionRoleList")
    @ApiOperation(value = "获取所有角色的权限列表")
    public AjaxResult<List<PermissionRoleListVO>> getPermissionRoleList() {
        return AjaxResult.data(permissionService.getPermissionRoleList());
    }

    @GetMapping("/getPermissionByRole")
    @ApiOperation(value = "获取指定角色的权限列表")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
    public AjaxResult<List<PermissionVO>> getPermissionByRole(Integer roleId) {
        return AjaxResult.data(permissionService.getPermissionByRole(roleId));
    }

    @GetMapping("/addAllControllerPermission")
    @ApiOperation(value = "新增所有Controller层接口到权限表")
    public AjaxResult<Boolean> addAllControllerPermission() {
        return AjaxResult.status(permissionService.addAllControllerPermission());
    }

    @GetMapping("/grantAnonymousPermission")
    @ApiOperation(value = "放行所有客户端的Controller")
    public AjaxResult<Boolean> grantAnonymousPermission() {
        return AjaxResult.status(permissionService.grantAnonymousPermission());
    }

    @GetMapping("/setAllPermission")
    @ApiOperation(value = "设置指定角色获取所有的权限（慎用）")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
    public AjaxResult<Boolean> setAllPermission(Integer roleId) {
        return AjaxResult.status(permissionService.setAllPermission(roleId));
    }

    @GetMapping("/cleanRolePermission")
    @ApiOperation(value = "清空系统的权限缓存")
    public AjaxResult<Boolean> clean() {
        return AjaxResult.status(permissionService.cleanRolePermission());
    }

}
